import 'package:flutter/material.dart';
import 'package:example/base/custom_dialog.dart';
import 'package:example/utils/common.dart';
import '../widget/base/dialog_router.dart';

extension CustomDialogExt on CustomDialog {
  Future<T?> show<T>({
    BuildContext? context,
    Color? backgroundColor,
    clickExternalClose = true,
    clickClose = false,
    RouteTransitionsBuilder? transitionsBuilder,
    DefaultAnim defaultAnim = DefaultAnim.Bottom,
  }) {
    return Navigator.push<T>(
      context ?? getCurContext(),
      DialogRouter<T>(
        this,
        backgroundColor: backgroundColor ?? Colors.black.withOpacity(0.55),
        clickExternalClose: clickExternalClose,
        clickClose: clickClose,
        transitionsBuilder: transitionsBuilder,
        defaultAnim: defaultAnim,
      ),
    );
  }

  void hide<T>({BuildContext? context, T? result}) {
    if (Navigator.canPop(context ?? getCurContext())) {
      Navigator.pop(context ?? getCurContext(), result);
    }
  }
}

Future<T?> showSheetDialog<T>(
  BuildContext context,
  Widget child, {
  bool scrollControlled = true,
  Color bodyColor = Colors.white,
  EdgeInsets? padding,
  BorderRadius? borderRadius,
}) {
  const radius = Radius.circular(16);
  borderRadius ??= const BorderRadius.only(topLeft: radius, topRight: radius);
  padding ??= const EdgeInsets.all(0);
  return showModalBottomSheet(
      context: context,
      elevation: 0,
      backgroundColor: bodyColor,
      shape: RoundedRectangleBorder(borderRadius: borderRadius),
      barrierColor: Colors.black.withOpacity(0.25),
      constraints: BoxConstraints(
          maxHeight: MediaQuery.of(context).size.height -
              MediaQuery.of(context).viewPadding.top),
      isScrollControlled: scrollControlled,
      builder: (ctx) => Container(
            padding: EdgeInsets.only(
              left: padding!.left,
              top: padding.top,
              right: padding.right,
              bottom: padding.bottom + MediaQuery.of(ctx).viewPadding.bottom,
            ),
            child: child,
          ));
}
